$(function(){
	//请求数据
	createMainTable({});

	//搜索确认按钮
	$("#confirmBtn").click(function(){
		createMainTable(); //请求数据
		mui.back(); //mui 返回
	});


	//选择器
	$(".myPicker").click(function(){
		var self = this;
		var nowDate = new Date();
		//创建普通的popPicker
		var popPicker = new mui.PopPicker();
		var popDataArray = new Array();
		//创建日期形式的Depicker
		var datePicker = new mui.DtPicker({
			type: "date",
			endDate: nowDate
		});
		switch ($(self).attr('id')){
			//开始日期选择器
			case 'selectorBeginDate':
				datePicker.setSelectedValue(getMonthStartDate());
				datePicker.show(function(items) {
					$("#selectorBeginDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//结束日期选择器
			case 'selectorEndDate':
				datePicker.show(function(items) {
					$("#selectorEndDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//清算状态
			case 'selectorClear':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '运行中'},{value: '0' ,text: '已完成'},{value: '-1' ,text: '已终止'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorClearShow").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			default:
				break;
		}
	});
})

//获取数据
function createMainTable(params){
	params = params || {};
	var data = {
		qryCode: $('#qryCodeS').val(),
		qryName: $('#qryNameS').val(),
		qryState: $("#selectorClearShow").attr('data-value'),
		qryStartDate: $('#selectorBeginDateShow').html(),
		qryEndDate: $('#selectorEndDateShow').html()
	}
	//显示当前查询条件
	$("#qryCode").html(data.qryCode);
	$("#qryName").html(data.qryName);
	$("#qryState").html($("#selectorClearShow").html());
	$("#beginDate").html(data.qryStartDate);
	$("#endDate").html(data.qryEndDate);


	$('#table').html("");
	layui.use('table', function () {
		var table = layui.table;
		table.render({
			elem: '#table'
			, limit: 20
			// ,skin: 'line'
			, even: true
			, size: 'sm'
			, height: $(window).height() - $('.head-search').height() - 60
			, url: url1 + Utils.Api.selfOrderGet //数据接口
			, where: $.extend({
				accessid: window.localStorage.accessid || '',
				field: 'createTime',
				direction: 'DESC'
			},data)
			, response: {
				statusName: 'hasError', //数据状态的字段名称，默认：code
				statusCode: false, //成功的状态码，默认：0
				msgName: 'error', //状态信息的字段名称，默认：msg
				countName: 'results', //数据总数的字段名称，默认：count
				dataName: 'rows' //数据列表的字段名称，默认：data
			}
			, page: false //开启分页
			, cols: [[ //表头
				{field: 'ordCode', title: '流程编号', align: 'center', width: 140},
				{field: 'ordName', title: '流程标题', align: 'center', width: 240, templet: function(d){
					return '<a class="color-blue" href="'+ d.ordActionUrl +'">'+ d.ordName +'</a>'
				}},
				{field: 'ordState', title: '状态', align: 'center', width: 100, templet: function(d){
					switch (d.ordState) {
						case 1:
							return '<span class="color-green">运行中</span>';
							break;
						case -1:
							return '<span class="color-red">已终止</span>';
							break;
						case 0:
							return '<span class="color-blue">已完成</span>';
							break;
						default:
							return '暂无'
					}
				}},
				{field: 'createTime', title: '创建时间', align: 'center',width: 120, templet: function (d) {
					return dateTimeFormat("yyyy-MM-dd", new Date(d.createTime))
				}},
				{field: 'modifyName', title: '最后办理人', align: 'center', width: 100},
				{field: 'modifyTime', title: '最后办理时间', align: 'center',width: 120, templet: function (d) {
					return dateTimeFormat("yyyy-MM-dd", new Date(d.modifyTime))
				}},
			]]
			,
		});
	})
}
